// SpecialToolDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SpecialTool.h"
#include "SpecialToolDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define COMMAND_TEST 401

#define TIMER_ID_TEST_01 100

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CSpecialToolDlg dialog




CSpecialToolDlg::CSpecialToolDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSpecialToolDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSpecialToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT2, m_EditHwnd);
	DDX_Control(pDX, IDC_LIST1, m_InfoList);
}

BEGIN_MESSAGE_MAP(CSpecialToolDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//ON_WM_MOUSEACTIVATE()
    ON_WM_TIMER()
	ON_MESSAGE(WM_HOTKEY, OnHotKey)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CSpecialToolDlg::OnBnClickedSetWindowPos)
	ON_BN_CLICKED(IDC_BUTTON2, &CSpecialToolDlg::OnBnClickedSetWindowPosAsync)
	ON_BN_CLICKED(IDC_SFGW, &CSpecialToolDlg::OnBnClickedSetForegroundWindow)
	ON_BN_CLICKED(IDC_SetWindowLongPtr, &CSpecialToolDlg::OnBnClickedSetwindowlongptr)
    ON_WM_SIZE()
END_MESSAGE_MAP()


// CSpecialToolDlg message handlers

BOOL CSpecialToolDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	ModifyStyleEx(0xFFFFFFFFL, WS_EX_LAYERED);//|WS_EX_NOACTIVATE); 

	::SetLayeredWindowAttributes(m_hWnd, 0, 255, LWA_ALPHA);
	
	VERIFY(RegisterHotKey(GetSafeHwnd(), COMMAND_TEST, 0, VK_F8));

    //SetTimer(TIMER_ID_TEST_01, 1000, NULL);

    SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)0, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSpecialToolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSpecialToolDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSpecialToolDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CSpecialToolDlg::CalculateHwnd()
{
	CString str;

	m_EditHwnd.GetWindowText(str);

	DWORD nHwnd = 0;
	TCHAR *p = str.GetBuffer();
	while (*p != '\0' && *p != ' ')
	{
		if (*p <= 'f' && *p >='a') 
		{
			nHwnd = (nHwnd << 4) + (*p - 'a') + 10;
		}
		else if (*p <='F' && *p >= 'A')
		{
			nHwnd = (nHwnd << 4) + (*p - 'A') + 10;
		}
		else if (*p <='9' && *p >= '0')
		{
			nHwnd = (nHwnd << 4) + (*p - '0');
		}
		else
		{
			nHwnd = 0;
			break;
		}
		++p;
	}

	m_ChangingHwnd = (HWND) nHwnd;
}

void CSpecialToolDlg::OnBnClickedSetWindowPos()
{
	// TODO: Add your control notification handler code here

	CalculateHwnd();

	if ( ::IsWindow(m_ChangingHwnd))
	{
        int x = rand() % 1000;
        int y = rand() % 800;
		::SetWindowPos(m_ChangingHwnd, m_hWnd, x, y, x, y, /*SWP_NOSIZE|SWP_NOMOVE|*/SWP_NOOWNERZORDER|SWP_NOACTIVATE);
	}

}

void CSpecialToolDlg::OnBnClickedSetWindowPosAsync()
{
	// TODO: Add your control notification handler code here
	
	CalculateHwnd();

	if ( ::IsWindow(m_ChangingHwnd))
	{
		::SetWindowPos(m_ChangingHwnd, m_hWnd, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_ASYNCWINDOWPOS);
	}

}

void CSpecialToolDlg::OnBnClickedSetForegroundWindow()
{
	// TODO: Add your control notification handler code here
	CalculateHwnd();

	if ( ::IsWindow(m_ChangingHwnd) && ::AllowSetForegroundWindow(::GetCurrentProcessId()))
	{
		::SetForegroundWindow(m_ChangingHwnd);
	}

}

LRESULT CSpecialToolDlg::OnHotKey(WPARAM wParam, LPARAM lParam)
{
	static unsigned char nAlpha = 0;
	static char step = 5;
	
	if ( 0 == nAlpha )
	{
		::GetLayeredWindowAttributes(m_hWnd, NULL, &nAlpha, NULL);
		if (nAlpha == 255)
		{
			step *= -1;
		}
	}

	nAlpha += step;
	nAlpha %= 256;

	step = (nAlpha == 0 || nAlpha >= 255) ? (-1*step) : (step);

	switch (wParam)
	{
	case COMMAND_TEST:
		{
			::SetLayeredWindowAttributes(m_hWnd, 0, nAlpha, LWA_ALPHA);
		}
		break;
	default:
		break;
	}
	return S_OK;
}

void CSpecialToolDlg::OnBnClickedSetwindowlongptr()
{
    ::MessageBox(NULL, L"xxx", L"MB_OK", MB_OK);
	// TODO: Add your control notification handler code here
}

int CSpecialToolDlg::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
	BOOL ret = ::SetForegroundWindow(m_hWnd);
	if (ret)
	{
		m_InfoList.InsertString(0, L"SFGW---True");
	}
	else
	{
		DWORD error = ::GetLastError();
		CString strError;
		strError.Format(L"Error Code : %d, %p", error, error);
		m_InfoList.InsertString(0, strError);
		m_InfoList.InsertString(0, L"SFGW---False");
	}
	return MA_ACTIVATE;
}

void CSpecialToolDlg::OnTimer(UINT_PTR nIDEvent)
{
    if (TIMER_ID_TEST_01 == nIDEvent)
    {
        OnBnClickedSetForegroundWindow();
    }
}
void CSpecialToolDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);

    // TODO: Add your message handler code here
}

void CSpecialToolDlg::OnCancel()
{
    // TODO: Add your specialized code here and/or call the base class

    CDialog::OnCancel();
}

void CSpecialToolDlg::OnOK()
{
    // TODO: Add your specialized code here and/or call the base class

    //CDialog::OnOK();
}
